import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:h_flutter_119/page/LoginPage.dart';
import 'package:h_flutter_119/page/getx/test/interceptor/LoginInterceptChain.dart';
import 'package:h_flutter_119/page/getx/test/interceptor/LoginNextInterceptor.dart';

// 登陆拦截器逻辑页面
class LoginInterceptorPage extends StatelessWidget{

  static BuildContext? _context;

  static goLoginPage(){
    Navigator.push(_context!, MaterialPageRoute(builder: (context) => LoginPage()));
  }

  @override
  Widget build(BuildContext context) {
    _context = context;
    return Container(
      child: Column(
        children: [
          ElevatedButton(onPressed: () {
            SpUtil.putString('token', '123456');
          }, child: const Text('模拟登陆，add token')),
          ElevatedButton(onPressed: () {
            SpUtil.remove('token');
          }, child: const Text('模拟异常，clear token')),
          ElevatedButton(onPressed: () {
            doLogin();
          }, child: const Text('模拟未登录，点击去登陆')),
        ],
      ),
      color: Colors.white,
    );
  }

  void doLogin() {
    Fluttertoast.showToast(msg: 'isLogin=${LoginInterceptChain.isLogin()}');
    LoginInterceptChain chain = LoginInterceptChain();
    chain.addInterceptor(LoginNextInterceptor((){
      Fluttertoast.showToast(msg: '恭喜你登陆成功，可以愉快的玩耍了');
    })).process();
  }

}